/**
 * @author Kristian Mandrup
 * @date May 14, 2009
 * @version 0.8
 * 
 * m3gen :: person select functions for person ruleSet in generatorRuleset
 * 
 * - Functions to set the values of a node object (nodeObj or jsTree node)
 */

var selectTitle = function (person, context, options) {
	context.eventController.handle('onBeginSelectTitle', context);
  	// if person has children
  	if (person.myChildren) {
		var boys = person.myChildren.boys || 0;
		var girls = person.myChildren.girls || 0;
		var children = boys.length + girls.length;
		if (children > 0) {
	    	return options.parent + ' ' + person.name.first + ' ' + options.children + ': ' + children;			
		}
	}
  	// if person has no children, choose head icon (male/female)
  	if (person.gender === 'male')
    	return options.male + ' ' + person.name.first;
  	else
    	return options.female +' ' + person.name.first;
}

/**
 * Contains json contentSelectionFunctions and options to be used for these functions
 * Options can contain any kind of helper content, such as static strings, complex objects or even 
 * additional helper functions!
 */
var personSelectFunctions = {
	options: {
		languages: {
			dk: {
				title: {
					male: 'Mand', female: 'Kvinde', parent: 'Foraeldre', children: 'boern'
				}
			},
			en: {
				title: {
					male: 'Male', female: 'Female', parent: 'Parent', children: 'children'
				}				
			},
			'default': {
				title: {
					male: 'Male', female: 'Female', parent: 'Parent', children: 'children'
				},
				icon: {
					femaleIcon: 'user_orange.png', maleIcon: 'user_green.png'
				},	
				attributes: {}								
			}
		},
		'id': {
			prefix: "person_"	
		},
		state: {
			defaultState: 'closed'
		},
		'metaData': {
			
		}
				
	},	
	
	selectLanguages: function(person, context){
		context.eventController.handle('onBeginSelectLanguages', context);
		var languages = {
			en: {},
			dk: {}
		};
		return languages;
	},	
	languages: {
		en: {
			selectTitle: selectTitle
		},		
		dk: {
			// personSelectFunctions
			selectTitle: selectTitle							
		},
		'default': {
			selectIcon: function(person, context, options){
				
				var callerObj = context.callerObj;
				var iconsPath = callerObj.getIconsPath();
				var nodeType = callerObj.getNodeType();
				var prefix = 'user/'
				// language specific persons!
				var lang = callerObj.getActiveLanguageKey();
				if (lang) {
					prefix += (lang + '/');
				}
				if (nodeType == m3gen.constants.nodeType.REFERENCE) {	
					prefix = 'ref_';
				}
			  	// if person has children choose folder icon with head
			  	if (person.children && person.children.length >0)
			    	return 'person/' + prefix + 'parent.png';
			  	// if person has no children, choose head icon (male/female)
				var icon = prefix + options.femaleIcon;
			  	if (person.gender === 'male') {
			    	icon = prefix + options.maleIcon;			    	
				}
				return callerObj.getFullIconsPath(icon);
			}
		}

	},

	selectAttributes: function (person, context) {
		context.eventController.handle('onBeginSelectAttributes', context);
		return {name: person.name, age: person.age};
	},
			
	selectId : function(person, context, options) {
		context.eventController.handle('onBeginSelectId', context);		
		var counter = context.globalExecState.activeCounter;
		var id = options.prefix + person.name.first + "_" + person.name.last;
		var uniqueId = id + '__' + counter;
		return {id: id, uniqueId: uniqueId};
	},
	
	/**
	 * 
	 * @param {Object} person
	 * @param {Object} context
	 * 
	 * 
	 */
	selectState : function(person, context, optionsState, optionsId) {
		context.eventController.handle('onBeginSelectState', context);		
		var oc = m3gen.util.oc;				
		var openList = context.openList || [];
		// 
		var id = personSelectFunctions.selectId(person, context, optionsId);
		if (id.id in oc(openList))
			return "open";
		else return context.defaultState || optionsState.defaultState || options.defaultState;
	},
	
	selectFriends : function(person, context){
		if (person.age > 18)
			return person.friends;
	},
	
	boy18Selector : function(person, context) {
		var children = person.myChildren || false;
		var result = [];
		if (children) {
			var boys = person.myChildren.boys || false;
			if (boys) {
				for each (var boy in boys){
					if (boy.age > 17)
						result.push(boy);
				}
			}
		}
		return result;
	}		
}


